const express = require('express');
const multer = require('multer');
const controller = require('../controllers/image.controller');

const router = express.Router();

// 配置 multer
const upload = multer({
    storage: multer.memoryStorage(), // 将文件存储在内存中
    limits: {
        fileSize: 10 * 1024 * 1024, // 限制文件大小为 10MB
        files: 1 // 限制每次上传一个文件
    },
    fileFilter: (req, file, cb) => {
        const allowedTypes = ['image/jpeg', 'image/png'];
        if (!allowedTypes.includes(file.mimetype)) {
            return cb(new Error('仅支持 JPEG/PNG 格式'));
        }
        cb(null, true);
    }
});

// 添加错误处理中间件
router.post('/analyze',
    (req, res, next) => {
        upload.single('image')(req, res, (err) => {
            if (err) {
                return res.status(400).json({
                    success: false,
                    error: err.message
                });
            }
            next();
        });
    },
    controller.analyze
);

module.exports = router;